package com.bdq.recommend.application.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.bdq.common.constants.Constant;
import com.bdq.common.entity.UserContext;
import com.bdq.common.entity.common.PageResult;
import com.bdq.common.enums.ChannelEnum;
import com.bdq.common.feign.content.param.ContentQueryParam;
import com.bdq.common.feign.content.vo.ContentListVO;
import com.bdq.common.utils.RedisUtil;
import com.bdq.common.utils.UserContextUtil;
import com.bdq.recommend.application.model.query.RecommendPageQry;
import com.bdq.recommend.application.service.impl.RecommendQueryService;
import com.bdq.recommend.infrastructure.config.TokenInterceptor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author malei
 * @date 2024-06-01 17:16
 */
@Service
public class RecommendQueryServiceImpl implements RecommendQueryService {

    @Resource
    private MyFeignClient myFeignClient;

    @Override
    public PageResult<ContentListVO> pageRecommend(RecommendPageQry qry) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        if (Objects.isNull(userContext)) {
            userContext = UserContext.defaultContext();
        }

        // 2 推荐页码
        if (ChannelEnum.RECOMMEND.getCode() == qry.getChannel()) {
            recommendPageHandle(userContext.getId(), qry);
        }

        // 3 检索内容ID
        ContentQueryParam queryParam = Convert.convert(ContentQueryParam.class, qry);
        queryParam.setUserId(userContext.getId());
        queryParam.setRegionLevel(userContext.getViewRegionLevel());
        queryParam.setRegionId(userContext.getViewRegionId());
        PageResult<Long> pageResult = myFeignClient.contentQuery(queryParam);

        // 4 内容
        if (CollUtil.isEmpty(pageResult.getList())) {
            if (ChannelEnum.RECOMMEND.getCode() != qry.getChannel()) {
                return PageResult.empty(pageResult.getTotal());
            }else {
                recommendPageUpdate(queryParam);
                pageResult = myFeignClient.contentQuery(queryParam);
                if (CollUtil.isEmpty(pageResult.getList())) {
                    return PageResult.empty(pageResult.getTotal());
                }
            }
        }
        List<ContentListVO> contentListVOS = myFeignClient.listContent(userContext.getId(), pageResult.getList());
        return new PageResult<>(contentListVOS, pageResult.getTotal());
    }

    /**
     * 推荐分页更新
     *
     * @param queryParam
     */
    private void recommendPageUpdate(ContentQueryParam queryParam) {
        RedisUtil.hPut(Constant.RedisKey.USER_RECOMMEND_PAGE_NO, String.valueOf(queryParam.getUserId()), String.valueOf(2));
        queryParam.setPageNo(1);
        queryParam.setPageSize(10);
    }

    /**
     * 推荐分页处理
     *
     * @param userId
     * @param qry
     */
    private void recommendPageHandle(Long userId, RecommendPageQry qry) {
        Object pageNoObj = RedisUtil.hGet(Constant.RedisKey.USER_RECOMMEND_PAGE_NO, String.valueOf(userId));
        if (Objects.isNull(pageNoObj)) {
            RedisUtil.hPut(Constant.RedisKey.USER_RECOMMEND_PAGE_NO, String.valueOf(userId), String.valueOf(2));
            qry.setPageNo(1);
            qry.setPageSize(10);
        }else {
            Integer pageNo = Integer.valueOf(String.valueOf(pageNoObj));
            qry.setPageNo(pageNo);
            qry.setPageSize(10);
            pageNo = pageNo + 1;
            RedisUtil.hPut(Constant.RedisKey.USER_RECOMMEND_PAGE_NO, String.valueOf(userId), String.valueOf(pageNo));
        }
    }
}
